import Vue from 'vue'
import VueI18n from 'vue-i18n'

//我的
import enUcenter from "./en/ucenter.js"; //中文
import cnUcenter from "./zh-Hans/ucenter.js"; //英文

//语言包引入 可多国语言
const langObj = {
	"zh-Hans": {
		...cnUcenter,
	},
	"en": {
		...enUcenter,
	},
}
Vue.use(VueI18n)


function loadLocaleMessages() {
	const locales = require.context('.', true, /[A-Za-z0-9-_,\s]+\/index\.js$/i)
	const messages = {}
	locales.keys().forEach(key => {
		let keyName = key.replace('./', '').replace('/index.js', '')
		messages[keyName] = Object.assign(locales(key).default, langObj[keyName])
	})
	return messages
}
const cur_lang = uni.getStorageSync('Uni-LocaleMessages') == 'en' ? 'en' : 'zh-Hans'
const locale = process.env.VUE_APP_I18N_LOCALE || (process.env.NODE_ENV === 'production' ? 'en' : 'zh-Hans')
const i18n = new VueI18n({
	locale: cur_lang || "zh-Hans",
	fallbackLocale: cur_lang || "zh-Hans",
	messages: loadLocaleMessages()
})

Vue.prototype._i18n = i18n
export default i18n